package com.szholly.pro.watergas_server.ps.monitor;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

import com.szholly.data.general.RowBase;
import com.szholly.pro.watergas_server.common.util.RedisHelper;
import org.springframework.stereotype.Component;

@Component
public class MonitorDataFormater {
	
	private static MonitorDataFormater _instance;
	
	public static MonitorDataFormater getInstance(){
		if(_instance == null){
			_instance = new MonitorDataFormater();
		}
		return _instance;
	}

	/**
	 * 地表水测量值小数位保留规则
	 */
	private HashMap<String, Integer> pollNumSW ;
	
	/**
	 * 污水测量值小数位保留规则
	 */
	private HashMap<String, Integer> pollNumWS ;
	
	/**
	 * 海口永庄测量值小数位保留规则
	 */
	private HashMap<String, Integer> pollNumHaiNan ;
	
	
	/**
	 * 供水测量值小数位保留规则
	 */
	private HashMap<String, Integer> pollNumSupplyWater ;
	
	/**
	 * 工业废水测量值小数位保留规则
	 */
	private HashMap<String, Integer> pollNumWasteWater ;

	/**
	 * 工业废水单位规则
	 */
	private HashMap<String, String> pollUnitWasteWater;
	
	public MonitorDataFormater(){
		pollNumSW = new HashMap<>();
		pollNumSW.put("011", 1);//COD
		pollNumSW.put("060", 4);//氨氮
		pollNumSW.put("001", 2);//pH
		pollNumSW.put("COND", 2);//电导率
		pollNumSW.put("DO", 3);//溶解氧
		pollNumSW.put("015", 3);//TOC
		pollNumSW.put("065", 3);//总氮
		pollNumSW.put("026", 3);//总砷
		pollNumSW.put("029", 3);//总铜
		pollNumSW.put("023", 3);//总铬
		pollNumSW.put("028", 2);//总镍
		pollNumSW.put("031", 2);//总锰
		pollNumSW.put("020", 3);//总汞
		pollNumSW.put("CN", 3);//氰化物
		pollNumSW.put("PHTEM", 1);//水温
		pollNumSW.put("101", 3);//总磷
		pollNumSW.put("NO3+",3);//硝酸盐
		pollNumSW.put("PHE",3);//挥发酚
		pollNumSW.put("H2PO4+",4);//磷酸盐
		pollNumSW.put("YD",3);//盐度
		pollNumSW.put("ORP",1);//ORP
		pollNumSW.put("CHLOROPHYLL_A",4);//叶绿素
		pollNumSW.put("WATER_DEPTH",4);//水位
		
		pollNumWS = new HashMap<>();
		pollNumWS.put("011", 2);//COD
		pollNumWS.put("060", 3);//氨氮
		pollNumWS.put("001", 2);//pH
		pollNumWS.put("101", 3);//总磷
		
		pollNumSupplyWater = new HashMap<>();
		pollNumSupplyWater.put("001", 2);
		pollNumSupplyWater.put("total_ci", 2);
		pollNumSupplyWater.put("p", 2);
		pollNumSupplyWater.put("ss", 2);
		
		pollNumHaiNan=new HashMap<>();
		pollNumHaiNan.put("PHTEM", 1);//水温
		pollNumHaiNan.put("001", 2);//PH
		pollNumHaiNan.put("COND", 0);//电导率
		pollNumHaiNan.put("SS", 1);//浊度
		pollNumHaiNan.put("060", 2);//氨氮
		pollNumHaiNan.put("011", 1);//高猛酸盐指数
		pollNumHaiNan.put("065", 2);//总氮
		pollNumHaiNan.put("101", 3);//总磷
		pollNumHaiNan.put("LEVEL_R", 3);//水位
		pollNumHaiNan.put("NO3", 3);//硝氮
		pollNumHaiNan.put("PHE", 5);//挥发酚
		pollNumHaiNan.put("CN", 2);//氟化物
		pollNumHaiNan.put("ROOM_TEM", 1);//温度
		pollNumHaiNan.put("ROOM_H", 1);//湿度
		pollNumHaiNan.put("CHLOROPHYLL_A",1);//叶绿素a
		pollNumHaiNan.put("ORP",0);//ORP
		pollNumHaiNan.put("BLUEGREEN_ALGAE",0);//蓝绿藻
		
		pollNumWasteWater = new HashMap<>();
		pollNumWasteWater.put("011", 2);//COD
		pollNumWasteWater.put("060", 2);//氨氮
		pollNumWasteWater.put("001", 2);//pH
		pollNumWasteWater.put("COND", 2);//电导率
		pollNumWasteWater.put("PHTEM", 2);//水温
		pollNumWasteWater.put("024", 3);//六价铬
		pollNumWasteWater.put("065", 3);//总氮
		pollNumWasteWater.put("029", 3);//总铜
		pollNumWasteWater.put("028", 3);//总镍
		pollNumWasteWater.put("031", 3);//总锰
		pollNumWasteWater.put("020", 3);//总汞
		pollNumWasteWater.put("101", 3);//总磷
		pollNumWasteWater.put("026", 3);//总砷
		pollNumWasteWater.put("023", 3);//总铬
		
		pollUnitWasteWater = new HashMap<>();
		pollUnitWasteWater.put("B01", "L/S");
	}
	
	/**
	 * 数据小数点格式化
	 * gislin
	 * 2016年5月26日
	 * @param list
	 * @return
	 */
	public MonitorDataModel numPointFormat(MonitorDataModel dataModel,String[] parms,String waterType) {
		
		String[] fieldPrixs = new String[]{"RTD","AVG","COU"};
		
		List<RowBase> list = dataModel.getData();
		
		for(String poll : parms){
			for(String fieldPrix:fieldPrixs){
				String fieldName  = fieldPrix+"_"+poll.toUpperCase();
				for(RowBase row : list){
					Object value = row.get(fieldName);
					String finalValue = NumPointFormat(poll, value,waterType);
					row.setValue(fieldName, finalValue);
				}
				RowBase minDataRow = dataModel.getMinData();
				minDataRow.setValue(fieldName, NumPointFormat(poll, minDataRow.get(fieldName),waterType));
				
				RowBase maxDataRow = dataModel.getMaxData();
				maxDataRow.setValue(fieldName, NumPointFormat(poll, maxDataRow.get(fieldName),waterType));
				
				RowBase avgDataRow = dataModel.getAvgData();
				avgDataRow.setValue(fieldName, NumPointFormat(poll, avgDataRow.get(fieldName),waterType));
			}
		}
		
		return dataModel;
	}
	
	/**
	 * 测量数值格式化，单个参数
	 * gislin
	 * 2016年5月26日
	 * @param poll
	 * @param value
	 * @param waterType 系统类型
	 * @return
	 */
	public String NumPointFormat(String poll,Object value,String waterType){
		
		if(value == null){
			return null;
		}
		
		BigDecimal doubleValue = null;
		
		try {
			doubleValue  =  new BigDecimal(value.toString());
		} catch (Exception e) {
			return value.toString();
		}
		
		HashMap<String, Integer> pollNum = new HashMap<>();
		
		if("1".equals(waterType)){
			pollNum = pollNumSW;
		}
		else if("0".equals(waterType)){
			pollNum = pollNumWS;
		}
		else if("3".equals(waterType)){
			pollNum = pollNumSupplyWater;
		}
		else if("7".equals(waterType)){
			pollNum = pollNumHaiNan;
		}
		else if("2".equals(waterType)){
			pollNum = pollNumWasteWater;
		}
		
		DecimalFormat df0 = new DecimalFormat("#0");
		DecimalFormat df1 = new DecimalFormat("#0.0");
		DecimalFormat df2 = new DecimalFormat("#0.00");
		DecimalFormat df3 = new DecimalFormat("#0.000");
		
		Integer pointNum = pollNum.get(poll.toUpperCase());
		String finalValue = null;
		
		if(pointNum == null){
			finalValue = df2.format(doubleValue.doubleValue());
			return finalValue;
		}
		if(pointNum == 0){
			finalValue = df0.format(doubleValue.doubleValue());
		}
		else if(pointNum == 1){
			finalValue = df1.format(doubleValue.doubleValue());
		}
		else if(pointNum == 2){
			finalValue = df2.format(doubleValue.doubleValue());
		}
		else if(pointNum == 3){
			finalValue = df3.format(doubleValue.doubleValue());
		}
		else if(pointNum == 4){
			finalValue = df3.format(doubleValue.doubleValue());
		}
		else if(pointNum == 5){
			finalValue = df3.format(doubleValue.doubleValue());
		}
		return finalValue;
	}
	
	/**
	 * 给报表添加报警信息
	 * gislin
	 * 2016年8月5日
	 * @param list 列表数据
	 * @param parms 要分析的污染物集合
	 * @param mn 设备编号
	 * @return
	 */
	public List<RowBase> generateAlarmInfo(List<RowBase> list,String[] parms,String mn){
		
		HashMap<String, Double> pollAlarmMap = new HashMap<>();
		
		for(RowBase row : list){
			
			for(String poll:parms){
				String fieldName  = "P"+poll.toUpperCase();
				Double value = row.getDouble(fieldName);
				
				row.setValue("P"+poll.toLowerCase()+"_ALARM_FLAG", 0);//0表示正常
				
				if(value == null){
					fieldName  = "P"+poll.toUpperCase()+"AVG";
					value = row.getDouble(fieldName);
				}
				
				if(value == null){
					fieldName  = poll.toUpperCase();
					value = row.getDouble(fieldName);
				}
				
				if(value == null){
					continue;
				}
				
				Double overUp = null;
				Double overDown = null;
				
				if(pollAlarmMap.containsKey(poll+"_alarmUp")){
					overUp = pollAlarmMap.get(poll+"_alarmUp");
				}
				else{
					overUp = RedisHelper.getInstance().getPollAlarmUp(mn, poll);
					pollAlarmMap.put(poll+"_alarmUp",overUp);
				}
				
				if(pollAlarmMap.containsKey(poll+"_alarmDown")){
					overDown = pollAlarmMap.get(poll+"_alarmDown");
				}
				else{
					overDown = RedisHelper.getInstance().getPollAlarmDown(mn, poll);
					pollAlarmMap.put(poll+"_alarmDown",overDown);
				}
				
				if(overUp != null && value > overUp){
					row.setValue("P"+poll.toLowerCase()+"_ALARM_FLAG", "ALARM_OVER");//表示超标
				}
				
				if(overDown != null && value < overDown){
					row.setValue("P"+poll.toLowerCase()+"_ALARM_FLAG", "ALARM_LESS");//表示小于低限
				}
				
				Double abnormalUp = null;
				Double abnormalDown = null;
				
				if(pollAlarmMap.containsKey(poll+"_abnormalUp")){
					abnormalUp = pollAlarmMap.get(poll+"_abnormalUp");
				}
				else{
					abnormalUp = RedisHelper.getInstance().getPollAbnormalUp(mn, poll);
					pollAlarmMap.put(poll+"_abnormalUp",abnormalUp);
				}
				
				if(pollAlarmMap.containsKey(poll+"_abnormalDown")){
					abnormalDown = pollAlarmMap.get(poll+"_abnormalDown");
				}
				else{
					abnormalDown = RedisHelper.getInstance().getPollAbnormalDown(mn, poll);
					pollAlarmMap.put(poll+"_abnormalDown",abnormalDown);
				}
				
				if(abnormalUp != null && value > abnormalUp){
					row.setValue("P"+poll.toLowerCase()+"_ALARM_FLAG", "ODD_OVER");//表示超高异常
				}
				
				if(abnormalDown != null && value < abnormalDown){
					row.setValue("P"+poll.toLowerCase()+"_ALARM_FLAG", "ODD_LESS");//表示超低异常
				}
			}
		}
		return list;
	}
	
	
	/**
	 * 数据单位格式化
	 * 张文成
	 * 2017年5月11日
	 * @param list
	 * @return
	 */
	public MonitorDataModel unitFormat(MonitorDataModel dataModel,String[] parms,String waterType) {
	   
		HashMap<String, PollutantsModel> unitMap = dataModel.getPollInfo();
		
		for(String poll : parms){
			PollutantsModel pm =  unitMap.get(poll);
			HashMap<String, String> pollNum = new HashMap<>();
	
			if("2".equals(waterType)){
				pollNum = pollUnitWasteWater;
			}
	        
			//pollNum中存在的才进行单位替换
			if(pollNum.containsKey(poll))
			{
				pm.unit = pollNum.get(poll);
			}
			
		}
		return dataModel;
	}
}
